import axios from "axios";
import qs from "qs";
import Config from "@/config/index";
const baseURL = Config.baseUrl;
class HttpRequest {
  constructor(baseUrl = baseURL) {
    this.baseUrl = baseUrl;
    this.queue = {};
  }
  getInsideConfig() {
    const config = {
      baseURL: this.baseUrl,
      headers: {
        "content-type": "application/x-www-form-urlencoded"
        // api_token: "777777"
      },
      method: "get"
    };
    return config;
  }
  interceptors(instance) {
    // 添加请求拦截器
    instance.interceptors.request.use(
      // 在发送请求之前做些什么
      config => {
        // todo 添加一个全局的loading...
        return config;
      },
      error => {
        // 对请求错误做些什么
        return Promise.reject(error);
      }
    );
    // 添加响应拦截器
    instance.interceptors.response.use(
      // 对响应数据做点什么
      res => {
        // console.log(res);
        return res.data;
      },
      // 对响应错误做点什么
      error => {
        return Promise.reject(error);
      }
    );
  }
  req(options) {
    const instance = axios.create();
    if (options.data) {
      let data = qs.stringify(options.data);
      options.data = data;
    }
    let reqOptions = Object.assign(this.getInsideConfig(), options);
    // console.log(reqOptions);
    this.interceptors(instance);
    return instance(reqOptions);
  }
}
export default HttpRequest;
